package web.gkw.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import web.gkw.dao.TechniqueDAO;
import web.gkw.model.Technique;

@Component("techniqueDAO")
public class TechniqueDAOImpl implements TechniqueDAO {
	
	private final static String hql_queryAll = 
			"from technique";
	private final static String hql_queryCount = 
			"select count(*) from technique";
	
	@Resource
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}


	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}


	@Override
	public boolean addTechnique(Technique tech) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().save(tech);
		return true;
	}


	@Override
	public List<Technique> getTechniqueFirst(int page) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql_queryAll);
		int begin = (page - 1) * 20;
		int end = page * 20;
		query.setFirstResult(begin);
		query.setMaxResults(end);
		return query.list();
	}


	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql_queryCount);
		int res = ((Long)query.uniqueResult()).intValue();
		return res;
	}


	@Override
	public Technique getTech(int id) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Technique res = (Technique) session.get(Technique.class, id);
		return res;
	}

}
